import numpy as np

# Mean Earth radius (km).
r_earth = 6371.0

# Conversion between nautical miles (nmi) and kilometers (km).
nmi2km = 1.852
km2nmi = 1.0 / nmi2km

# Maximum number of ships that can be attached to an icebreaker.
max_ships = 3

# Time.
Time = np.datetime64


def angular_distance(lat1, lon1, lat2, lon2):
    """
    Compute the angular distance between two points.

    Args:
        lat1: Latitude of the first point (deg).
        lon1: Longitude of the first point (deg).
        lat2: Latitude of the second point (deg).
        lon2: Longitude of the second point (deg).

    Returns:
        The angular distance (deg).

    References:
        https://en.wikipedia.org/wiki/Angular_distance
    """
    phi1 = np.deg2rad(lat1)
    lmb1 = np.deg2rad(lon1)
    phi2 = np.deg2rad(lat2)
    lmb2 = np.deg2rad(lon2)
    cos_theta = np.sin(phi1) * np.sin(phi2) + np.cos(phi1) * np.cos(phi2) * np.cos(lmb1 - lmb2)
    theta = np.arccos(cos_theta)
    return np.rad2deg(theta)


def path_lenghth(lat1, lon1, lat2, lon2):
    """
    Compute the length of straight path between two points.

    Args:
        lat1: Latitude of the first point (deg).
        lon1: Longitude of the first point (deg).
        lat2: Latitude of the second point (deg).
        lon2: Longitude of the second point (deg).

    Returns:
        The path length (nmi).
    """
    theta = angular_distance(lat1, lon1, lat2, lon2)
    return km2nmi * np.pi * r_earth * (theta / 180)
